package com.hongfeng.support.util.json;

import java.util.Date;
import java.util.List;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;

import com.hongfeng.support.model.Menu;

public class JsonUtils {

	/**
	 * array to json
	 * 
	 * @param obj
	 */
	public static String array2Json(Object obj, String... excludes) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(excludes);
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor());
		JSONArray jsonArray = JSONArray.fromObject(obj, jsonConfig);
		return jsonArray.toString();
	}

	/**
	 * bean to json
	 * 
	 * @param obj
	 */
	public static String bean2Json(Object obj, String... excludes) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(excludes);
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor());
		JSONObject jsonObject = JSONObject.fromObject(obj, jsonConfig);
		return jsonObject.toString();
	}

	/**
	 * bean to json
	 * 
	 * @param json
	 * @param clazz
	 * @param replaceMap
	 *            有些变量的值要替换成json
	 * @return
	 */
	public static Object json2Object(String json, Class<?> clazz, String... excludes) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(excludes);
		JSONObject jsonObject = JSONObject.fromObject(json);
		jsonObject = JSONObject.fromObject(jsonObject.toString(), jsonConfig);
		JSONUtils.getMorpherRegistry().registerMorpher(
				new DateMorpher(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" }));
		Object obj = JSONObject.toBean(jsonObject, clazz);
		return obj;
	}

	/**
	 * json to jsonobject
	 * 
	 * @param json
	 * @return
	 */
	public static JSONObject json2Object(String json) {
		JSONObject jsonObject = JSONObject.fromObject(json);
		jsonObject = JSONObject.fromObject(jsonObject.toString());
		JSONUtils.getMorpherRegistry().registerMorpher(
				new DateMorpher(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" }));
		return jsonObject;
	}

	/**
	 * 菜单转换成树形所需json数据 [ {id: "user", text: "用户管理"},
	 * 
	 * {id: "lists", text: "Lists", pid: "user" },
	 * 
	 * {id: "datagrid", text: "DataGrid", pid: "lists"}, {id: "tree", text:
	 * "Tree" , pid: "lists"}, {id: "treegrid", text: "TreeGrid " , pid:
	 * "lists"}]
	 * 
	 * @param menus
	 * @return
	 */
	public static String treeJson(List<Menu> menus, boolean isAddHead) {
		if (menus == null) {
			return null;
		}
		if (menus.size() == 0) {
			return "[]";
		}
		for (Menu menu : menus) {
			if (menu.getPid() != null) {
				for (Menu m : menus) {
					if (menu.getPid().longValue() == m.getId().longValue()) {
						menu.setParentMenu(m);
						break;
					}
				}
			}
		}
		StringBuffer tree = new StringBuffer("[");
		if (isAddHead) {
			tree.append("{id:\"sys_menu_0\",text:\"顶级菜单\"},");
		}
		for (Menu menu : menus) {
			tree.append("{id:\"").append(menu.getMenuCode()).append("_").append(menu.getId()).append("\"")
					.append(",text:\"").append(menu.getMenuName()).append("\"");
			if (menu.getPid() != null) {
				Menu pMenu = menu.getParentMenu();
				tree.append(",pid:\"").append(pMenu.getMenuCode()).append("_").append(pMenu.getId()).append("\"");
			} else {
				tree.append(",pid:\"sys_menu_0\"");
			}
			tree.append("},");
		}
		return tree.substring(0, tree.length() - 1) + "]";
	}

	/**
	 * 
	 *  <加载左边菜单树 >
	 *
	 *	@param menus
	 *	@return
	 *	String
	 *	@see
	 * 	@throws
	 */
	public static String loadMenuTree(List<Menu> menus) {
		if (menus == null) {
			return null;
		}
		if (menus.size() == 0) {
			return "[]";
		}
		StringBuffer tree = new StringBuffer("[");
		for (Menu menu : menus) {
			tree.append("{id:\"").append(menu.getMenuCode()).append("\"").append(",text:\"").append(menu.getMenuName())
					.append("\"");
			if (menu.getPid() != null) {
				Menu pMenu = menu.getParentMenu();
				tree.append(",pid:\"").append(pMenu.getMenuCode()).append("\"");
			}
			tree.append("},");
		}
		return tree.substring(0, tree.length() - 1) + "]";
	}

}
